args Controls CreativityMeasure 


local lab p_CreatOcc

preserve

* setup
replace Creativity=creativityt_`CreativityMeasure'_7


local ipw=1

local  CreatOcc     CreatOcc_I33    CreatOcc_I42  CreatOcc_I46   CreatOcc_I50   CreatOcc_I55

su  `CreatOcc'

* calculate ipw weights
foreach y of local  CreatOcc {
	if `ipw'==1{
		ge io=`y'!=.
		qui logit io $HDC_0
		drop io
		predict p_`y', pr
	}

	if `ipw'==0{
		ge  p_`y'=1
	}

}	

* reshape to long dataset
reshape long CreatOcc_I p_CreatOcc_I SE_I cCSC_1_I cCSC_2_I cCSC_3_I cCSC_4_I cCSC_5_I cCSC_6_I cCSC_7_I, i(NCDSID) j(age)

************ all. ************************************************************** 

* effect on  CreatOcc
rifhdreg  CreatOcc  `Controls'  [pweight=1/p_`version'CreatOcc], rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc1_All


* controlling for education
rifhdreg  CreatOcc  `Controls' mQual16 mQual18 mUniQual MissingQual16 MissingQual18 MissingUniQual [pweight=1/p_`version'CreatOcc], rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc2_All

* controlling for occupational category
rifhdreg  CreatOcc  `Controls' cCSC* [pweight=1/p_CreatOcc], rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc3_All


************ employees **********************************************************



* effect on  CreatOcc
rifhdreg  CreatOcc  `Controls'  [pweight=1/p_CreatOcc] if SE_I==0, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc1_Emp


* controlling for education
rifhdreg  CreatOcc  `Controls' mQual16 mQual18 mUniQual MissingQual16 MissingQual18 MissingUniQual [pweight=1/p_`version'CreatOcc]  if SE_I==0, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc2_Emp

* controlling for occupational category
rifhdreg  CreatOcc  `Controls' cCSC* [pweight=1/p_CreatOcc]  if SE_I==0, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc3_Emp


************ self-employeed *****************************************************



* effect on  CreatOcc
rifhdreg  CreatOcc  `Controls'  [pweight=1/p_CreatOcc] if SE_I==1, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc1_SE


* controlling for education
rifhdreg  CreatOcc  `Controls' mQual16 mQual18 mUniQual MissingQual16 MissingQual18 MissingUniQual [pweight=1/p_CreatOcc]  if SE_I==1, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc2_SE

* controlling for occupational category
rifhdreg  CreatOcc  `Controls' cCSC* [pweight=1/p_CreatOcc]  if SE_I==1, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_CreatOcc3_SE








local text  prefoot( \cline{2-4}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-4} )  postfoot(\vspace{-.3cm} \\  ) 

* Stats
local ph  ""
local stP "`ph' stats( N mdv, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  "Mean dep. var."))" 



* all individuals in work 
******************************************************************************************

* without p-values
esttab es*_All using results/Tables/R_OLS_`lab'_All.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) frag `stP' ///
	mtitles("(1)" "(2)" "(3)")  `text'


	
* with p values
esttab es*_All using results/Tables/R_OLS_`lab'p_All.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) frag `stP' ///
	mtitles("(1)" "(2)" "(3)") `text'	
	
	
* employed
******************************************************************************************

* without p-values
esttab es*_Emp using results/Tables/R_OLS_`lab'_Emp.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) frag `stP' ///
	mtitles("(1)" "(2)" "(3)")  `text'


	
* with p values
esttab es*_Emp using results/Tables/R_OLS_`lab'p_Emp.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) frag `stP' ///
	mtitles("(1)" "(2)" "(3)") `text'	
	
* self-employed
******************************************************************************************

* without p-values
esttab es*_SE using results/Tables/R_OLS_`lab'_SE.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) frag `stP' ///
	mtitles("(1)" "(2)" "(3)")  `text'


	
* with p values
esttab es*_SE using results/Tables/R_OLS_`lab'p_SE.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) frag `stP' ///
	mtitles("(1)" "(2)" "(3)") `text'		
	
	
	

	
est drop _all
restore
